package Tool;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class WriteLog {

    public static void write(String s, int machineNum) {

        String testunite = "TestUnit";
        String testunitenum = String.valueOf(machineNum);

        // 获得当前系统时间,time1构造文件名，time2构造文件里的具体时间
        Date date1 = new Date();
        SimpleDateFormat df1 = new SimpleDateFormat("yyyy-MM-dd");
        String time1 = df1.format(date1);
        Date date2 = new Date();
        SimpleDateFormat df2 = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String time2 = df2.format(date2);
        // 使用相对路径，日志文件在项目logs内
        String outestpath = "D:\\logs";
        String testunitepath = testunite + testunitenum;
        String dir = outestpath + "/" + testunitepath;

        File filedir = new File(dir);
        if (!filedir.exists()) {
            filedir.mkdirs();
        }
        String filename = time1 + ".txt";
        String path = dir + "/" + filename;
        File file = new File(path);
        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        Writer out = null;
        try {
            // 使用字符流
            out = new FileWriter(path, true);
            // 注意反斜杠的方向，/r是回车，/n是换行
            out.write(time2 + ":" + s + ";" + "\r\n");
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
//	public static void main(String[] args) {
//		write("kaishijiance", "1");
//	}
}
